import { utilities } from 'nest-winston';
import { createLogger } from 'winston';
import * as winston from 'winston';
import 'winston-daily-rotate-file';

export const logInstance = createLogger({
  // 日志选项
  transports: [
    new winston.transports.Console({
      silent: process.env.NODE_ENV === 'prod',
      level: 'info',
      format: winston.format.combine(
        winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
        utilities.format.nestLike(),
      ),
    }),

    // warn error 日志存储到 logs/application - 日期.log 文件中
    new winston.transports.DailyRotateFile({
      level: 'error',
      dirname: 'logs',
      filename: 'error-%DATE%.log',
      datePattern: 'YYYY-MM-DD-HH',
      zippedArchive: true,
      maxSize: '10m',
      maxFiles: '14d',
      format: winston.format.combine(
        winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
        winston.format.simple(),
      ),
    }),

    // info 日志存储到 logs/info - 日期.log 文件中
    new winston.transports.DailyRotateFile({
      level: 'info',
      dirname: 'logs',
      filename: 'info-%DATE%.log',
      datePattern: 'YYYY-MM-DD-HH',
      zippedArchive: true,
      maxSize: '10m', // 文件大小
      maxFiles: '14d',
      format: winston.format.combine(
        winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
        winston.format.simple(),
      ),
    }),
  ],
});
